package com.yanqu.road.server.manger;

import com.yanqu.road.entity.config.goods.OpenGoodsBagResult;
import com.yanqu.road.entity.wingroom.workshop.WorkShopItem;
import com.yanqu.road.logic.config.GameConfig;
import com.yanqu.road.utils.property.Property;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author ：zfr
 * @date ：2022/3/20 16:16
 */
public class WorkShopCommonMgr {
    public static List<WorkShopItem> newPool() {
        String refreshBoxRule = GameConfig.WINGROOM_WORKSHOP_REFRESH_BOX_RULE;
        String[] boxArray = refreshBoxRule.split("\\|");
        List<WorkShopItem> workShopItemList = new ArrayList<>();
        for (String boxParam : boxArray) {
            String[] boxParamArray = boxParam.split(",");
            int bagId = Integer.parseInt(boxParamArray[0]);
            int itemCount = Integer.parseInt(boxParamArray[1]);
            List<OpenGoodsBagResult> goodsBagResultList = TempGoodsBagMgr.getMultipleRandomGoodsNoRepeat(bagId, itemCount);
            for (OpenGoodsBagResult openGoodsBagResult : goodsBagResultList) {
                WorkShopItem item = constructItem(openGoodsBagResult);
                if (item != null) {
                    workShopItemList.add(item);
                }
            }
        }
        return workShopItemList;
    }

    private static WorkShopItem constructItem(OpenGoodsBagResult result) {
        Property reward = result.getReward();

        if (reward.isNothing()) {
            return null;
        }

        WorkShopItem workShopItem = new WorkShopItem();

        for (Map.Entry<Integer, BigInteger> entry : reward.getGoods().entrySet()) {
            workShopItem.setFurnitureId(entry.getKey());
            workShopItem.setCount(entry.getValue().longValue());
        }

        return workShopItem;
    }
}
